from common.readConfig import read_conf
import re
import random
from common.mysql import ReadSQL

"""
通过search方法进行匹配
"""
class TempData():
    """用来临时保存接口之间依赖参数的类"""
    pass


def data_replace(data):
    """读取配置文件替换指定参数"""
    while re.search(r'#(.+?)#', data):
        res = re.search(r'#(.+?)#', data)
        # 提取要替换的内容
        r_data = res.group()
        # 获取要替换的字段
        key = res.group(1)
        # 通过获取的字段，去配置文件中读取对应的配置选项内容
        try:
            value = read_conf.get('data', key)
        except:
            value = getattr(TempData,key)

        # 进行替换
        data = re.sub(r_data, value, data)
    return data


def random_phone():
    """随机生成手机号替换动态参数"""
    db = ReadSQL()
    while True:
        phone = '13'
        for i in range(9):
            num = random.randint(0, 9)
            phone += str(num)
        # 数据库中查找该手机号是否存在
        sql = "select * from member where MobilePhone='{}';".format(phone)
        if not db.find_one(sql):
            return phone







if __name__ == '__main__':
    # data = '{"mobilephone": "#phone#","pwd":"#pwd#"}'
    # res = data_replace(data)
    # print(res)
    res1 = random_phone()
    print(res1)

# data = '{"mobilephone": "#phone#","pwd":"#pwd#"}'
#
# # 判断是否有匹配到数据
# while re.search(r'#(.+?)#',data):
#     res = re.search(r'#(.+?)#',data)
#     # 获取匹配到的内容
#     r_data = res.group()
#     # 提取要替换的字段
#     key = res.group(1)
#
#     # 通过提取的字段，去配置文件中读取对应的配置选项的内容
#     phone = myconf.get('data',key)
#
#     # 进行替换操作
#     data = re.sub(r_data,phone,data)
# print(data)
